﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using System.Web.Security;
using WCF;


namespace geekmeet
{
    /// <summary>
    /// This class contains methods for dynamic update of the page content. 
    /// It gets data from the database via our webservice, and serialize that data
    /// to JSON, which it presented to the client. This class is used primary for 
    /// updating the wall/friend/message-icons from javascript, and is also used 
    /// when sending private messages for direct validation from the client to 
    /// the database.
    /// </summary>
    public partial class ajax : System.Web.UI.Page
    {
        GeekMeetService wcf = new GeekMeetService();
        Guid loggedInUserGUID;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                loggedInUserGUID = (Guid)Membership.GetUser().ProviderUserKey;

                //Determines what type of call the javascript is calling for
                //and executes proper method
                if (Request.QueryString["call"] != null &&
                    Request.QueryString["call"].Equals("profile")
                    && Request.QueryString["id"] != null)
                {
                    FetchProfile();
                }

                if ((Request.QueryString["call"] != null &&
                    Request.QueryString["call"].Equals("notifications"))
                     && Request.QueryString["id"] != null)
                {
                    GetNotifications();
                }
                if ((Request.QueryString["call"] != null &&
                   Request.QueryString["call"].Equals("FindUser"))
                    && Request.QueryString["query"] != null)
                {
                    FindUser();
                }
                if ((Request.QueryString["call"] != null &&
                   Request.QueryString["call"].Equals("newFriends"))
                    )
                {
                    //GetNewFriends();????
                }
            }
        }

        /// <summary>
        /// Checks if a user exists and presents a JSON-object of that user to the client
        /// </summary>
        /// <returns>True if the user is found</returns>
        private bool FindUser()
        {
            string query = Request.QueryString["query"];
            ProfileComposite foundUser = wcf.FindUser(query);

            if (foundUser != null)
            {
                JavaScriptSerializer oSerializer =
                            new JavaScriptSerializer();
                string sJSON = oSerializer.Serialize(foundUser);

                Response.Clear();
                Response.ContentType = "application/json; charset=utf-8";
                Response.Write(sJSON);
                Response.End();

                return true;
            }

            else
            {
                return false;
            }
        }

        /// <summary>
        /// Checks if the user has any new messages/friend requests/wallposts, and presents the
        /// new objects as JSON.
        /// </summary>
        private void GetNotifications()
        {
            try
            {
                Guid requestedID = Guid.Parse(Request.QueryString["id"]);
                //Uses a wcf-method that returns a dictionary with the notification type string
                //as a key and a bool value that determines if there is any unseen notifications
                Dictionary<string, bool> result = (Dictionary<string, bool>)wcf.GetNotifications(loggedInUserGUID);

                JavaScriptSerializer oSerializer =
                        new JavaScriptSerializer();
                string sJSON = oSerializer.Serialize(result);

                Response.Clear();
                Response.ContentType = "application/json; charset=utf-8";
                Response.Write(sJSON);
                Response.End();

            }
            catch
            {
            }
        }

        /// <summary>
        /// Gets a profile and presents it as JSON
        /// </summary>
        public void FetchProfile()
        {
            try
            {
                Guid requestedID = Guid.Parse(Request.QueryString["id"]);
                ProfileComposite p = wcf.GetProfile(requestedID);

                JavaScriptSerializer oSerializer =
                    new JavaScriptSerializer();
                string sJSON = oSerializer.Serialize(p);

                Response.Clear();
                Response.ContentType = "application/json; charset=utf-8";
                Response.Write(sJSON);
                Response.End();
            }
            catch (Exception ex)
            {
                
            }
        }
    }
}
